package com.fcscs.cservice.client.main.configuration.inroomdining;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreConstants.ValidationListener;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.MessageBoxTools;
import com.fcscs.cservice.client.common.controller.InRoomServiceController;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.domain.BillOption;
import com.fcscs.share.gwt.common.ShareGWTConstants;

public class BillOptionDialog extends BaseFormDialog<BillOption> implements
FormSubmitListener<BillOption>{

	private static BillOptionDialog INSTANCE;
	private MyFormPanel myFormPanel;
	private HorizontalPanel hp1;
	private LayoutContainer lc1;
	private TextField<String> billOptionField;
	private HorizontalPanel hp2;
	private LayoutContainer lc2;
	private TextField<String> actionField;
	private FormButton saveBtn;
	private FormButton cancelBtn;
	private boolean isAdd;
	private FormSubmitListener<BillOption> listener;
	private PropertyIdNameModelData currentProperty;

	public static BillOptionDialog getInstance(boolean isAdd,FormSubmitListener<BillOption> listener,PropertyIdNameModelData propertyModel) {

		if (null == INSTANCE) {
			INSTANCE = new BillOptionDialog();
		}
		INSTANCE.clear();
		INSTANCE.listener=listener;
		INSTANCE.isAdd=isAdd;
		INSTANCE.currentProperty=propertyModel;
		INSTANCE.setHeading(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.add(),
				CServiceConstants.CSERVICE_CONSTANTS.billOption()));
		return INSTANCE;
	}

	public BillOptionDialog() {
		myFormPanel = new MyFormPanel(null);
		add(myFormPanel);
		setSize(450,180);
	}

	public class MyFormPanel extends FcsFormPanel<BillOption> {
		@Override
		protected void delayValidation(final ValidationListener l) {
			isBillOptionTaken(l);
		}

		protected MyFormPanel(FormSubmitListener<BillOption> l) {
			super(l);
			setHeaderVisible(false);

			VerticalPanel vp = new VerticalPanel();
			vp.setSpacing(5);
			vp.setId("BillOptionDialog_vp");

			hp1 = new HorizontalPanel();
			hp1.setId("BillOptionDialog_hp1");
			vp.add(hp1);

			lc1 = new LayoutContainer();
			lc1.setId("BillOptionDialog_lc1");
			lc1.setLayout(new ColumnLayout());
			hp1.add(lc1);

			Label billLabel = new Label(CServiceConstants.CSERVICE_CONSTANTS.billOption()
					+ CServiceConstants.FIELD_SEP);
			billLabel.setId("BillOptionDialog_billLabel");
			lc1.add(billLabel, new ColumnData(100));

			billOptionField = new TextField<String>();
			billOptionField.setId("BillOptionDialog_billOptionField");
			billOptionField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					isBillOptionTaken(null);
				}
			});
			lc1.add(billOptionField, new ColumnData(200));

			hp2 = new HorizontalPanel();
			hp2.setId("BillOptionDialog_hp2");
			vp.add(hp2);

			lc2 = new LayoutContainer();
			lc2.setId("BillOptionDialog_lc2");
			lc2.setLayout(new ColumnLayout());
			hp2.add(lc2);

			Label actionLabel = new Label(CoreConstants.CORE_CONSTANTS.code() + CServiceConstants.FIELD_SEP);
			actionLabel.setId("BillOptionDialog_actionLabel");
			lc2.add(actionLabel, new ColumnData(100));

			actionField = new TextField<String>();
			actionField.setId("BillOptionDialog_actionField");
			lc2.add(actionField, new ColumnData(200));

			saveBtn = new FormButton(CoreConstants.CORE_CONSTANTS.save());
			saveBtn.setId("BillOptionDialog_saveBtn_Id");
			saveBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

				@Override
				public void componentSelected(ButtonEvent ce) {

					isValid(new ValidationListener() {

						@Override
						public void onValidate(boolean good) {
								submit(SubmitType.DEFAULT);
								saveBtn.removeStyleName(CoreConstants.CSS_FORM_BUTTON_FOCUS);
						}
					});
				}
			});
			getButtonBar().add(saveBtn);

			cancelBtn = new FormButton(CoreConstants.CORE_CONSTANTS.cancel());
			cancelBtn.setId("BillOptionDialog_cancelBtn_Id");
			cancelBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				
				@Override
				public void componentSelected(ButtonEvent ce) {
					BillOptionDialog.this.hide();
					cancelBtn.removeStyleName(CoreConstants.CSS_FORM_BUTTON_FOCUS);
				}
			});
			getButtonBar().add(cancelBtn);
			
			add(vp);

		}

		@Override
		public void updateForm(BillOption entity) {
			if(isAdd){
				
			}else{
			billOptionField.setValue(entity.getName());
			actionField.setValue(entity.getNameSecLang());
			}
		}

		@Override
		public void populateEntity(BillOption entity) {

		}

		@Override
		public void setFocus() {

		}
		private void isBillOptionTaken(final ValidationListener l){
			InRoomServiceController.getINSTANCE().isBillOptionTaken(SessionContext.getInstance().getOrgId(), currentProperty.getEntity().getId(), billOptionField.getValue(), 0L, new AsyncCallbackListener<Boolean>(){

				@Override
				public void onLogin(String username) {
					InRoomServiceController.getINSTANCE().isBillOptionTaken(SessionContext.getInstance().getOrgId(),currentProperty.getEntity().getId(), billOptionField.getValue(), 0L,this);
				}

				@Override
				public void onFailure(Throwable caught) {
					MessageBoxTools.messageAlertSystemError();
				}

				@Override
				public void onSuccess(Boolean result) {
					if(result){
						billOptionField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.isTaken(billOptionField.getValue().trim()));
					}else{
						billOptionField.clearInvalid();
					}
					if(l!=null){
						l.onValidate(!result);
					}
				}
				
			});
		}

	}
	
	@Override
	public void hide() {
		super.hide();
	}
	
	public void clear(){
		billOptionField.clear();
		actionField.clear();
	}

	@Override
	protected FcsFormPanel<BillOption> getFormPanel() {
		return myFormPanel;
	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<BillOption> form,
			SubmitType submitType) {
		if(listener!=null){
			listener.formSubmitCallBack(form, submitType);
		}
	}


}
